package com.liuhouse.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liuhouse.constants.SystemCanstants;
import com.liuhouse.pojo.Link;
import com.liuhouse.mapper.LinkMapper;
import com.liuhouse.service.ILinkService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liuhouse.utils.BeanCopyUtils;
import com.liuhouse.utils.ResponseResult;
import com.liuhouse.vo.LinkVo;
import com.liuhouse.vo.PageVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 友链 服务实现类
 * </p>
 *
 * @author liuhouse
 * @since 2024-01-27
 */
@Service
public class LinkServiceImpl extends ServiceImpl<LinkMapper, Link> implements ILinkService {
    @Override
    public ResponseResult getAllLink() {
       //构建查询条件
        LambdaQueryWrapper<Link> linkLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //查询所有审核通过的友情链接
        linkLambdaQueryWrapper.eq(Link::getStatus , SystemCanstants.LINK_STATUS_NORMAL);
        //进行友情链接查询
        List<Link> link_list = list(linkLambdaQueryWrapper);
        //进行vo转换
        List<LinkVo> linkVos = BeanCopyUtils.copyBeanList(link_list, LinkVo.class);
        //包装返回
        return ResponseResult.okResult(linkVos);
    }


    //查询链接分页
    @Override
    public PageVo selectLinkPage(Link link, Integer pageNum, Integer pageSize) {
        //构建查询条件
        LambdaQueryWrapper<Link> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(link.getName()) , Link::getName , link.getName());
        queryWrapper.eq(Objects.nonNull(link.getStatus()) , Link::getStatus , link.getStatus());
        //进行分页配置
        Page<Link> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        //进行链接的分页查询
        page(page , queryWrapper);
        //转换成VO然后返回
        List<Link> records = page.getRecords();
        PageVo pageVo = new PageVo();
        pageVo.setTotal(page.getTotal());
        pageVo.setRows(records);
        return pageVo;
    }
}
